package qp.src.app.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubDataBeanDao {

  private static Map<Integer/*ID*/, SubDataBean> subDataBeanStore = new HashMap<Integer, SubDataBean>();
  private Integer id = 0;

  public SubDataBeanDao( List<DataBean> datas ){
    for( DataBean data : datas ){
      SubDataBean subData = data.getData();
      if( subData != null ){
        persist(subData);
      }
    }
  }

  public void persist(SubDataBean subDataBean){
    System.out.println("--SERVER SubDataBeanDao persists");
    if( subDataBean.getId() == null ){
      subDataBean.setId(id++);
      subDataBean.setVersion(1);
    }
    else{
      subDataBean.setVersion( subDataBean.getVersion() + 1 );
    }

    //-------------------------------
    SubDataBean existing = subDataBeanStore.get( subDataBean.getId() );
    System.out.println("--SERVER SubDataBeanDao persist subDataBean");
    if( existing != null ){
      System.out.println("--SERVER SubDataBeanDao persist subDataBean name"+subDataBean.getName());
      existing.copyFrom( subDataBean );
    }
    else {
      System.out.println("--SERVER SubDataBeanDao persist subDataBean !!ELSE name"+subDataBean.getName()+ "   id = "+subDataBean.getId());
      subDataBeanStore.put(subDataBean.getId(), subDataBean);
    }
  }

  public void delete(Integer id){
    System.out.println("--SERVER SubDataBeanDao delete "+id);
    if( id != null ){
      System.out.println("--SERVER SubDataBeanDao delete start subDataBeanStore"+subDataBeanStore.get(id));
      subDataBeanStore.remove( id );
      System.out.println("--SERVER SubDataBeanDao delete after subDataBeanStore"+subDataBeanStore.get(id));
    }
  }

  public List<SubDataBean> getSubDataBean(){
    List<SubDataBean> result = new ArrayList<SubDataBean>();
    if( subDataBeanStore.keySet().size() == 0 ){
      initSubDataBeanStore();
    }
    for( Integer id : subDataBeanStore.keySet() ){
      result.add( subDataBeanStore.get(id) );
    }
    System.out.println("--SERVER SubDataBeanDao getSubDataBean result size"+result.size());
    return result;
  }

  public SubDataBean findSubDataBean(Integer id){
    System.out.println("--SERVER SubDataBeanDao findSubDataBean id"+id );
    SubDataBean subDataBean = null;
    if( id != null ){
      subDataBean = subDataBeanStore.get(id);
    }
    if( subDataBean != null ){
      System.out.println("--SERVER SubDataBeanDao findSubDataBean subDataBean"+subDataBean.getName() );
    }
    System.out.println("--SERVER SubDataBeanDao findSubDataBean subDataBean"+subDataBean );
    return subDataBean;
  }

  private void initSubDataBeanStore(){
    System.out.println("--SERVER initSubDataBeanStore ");
    for(int i = 0; i < 5; i++){
      SubDataBean subDataBean = new SubDataBean();
      subDataBean.setId(id);
      subDataBean.setVersion( 1 );
      subDataBean.setLastName("Coolper"+i);
      subDataBean.setName("Bill"+i);
      id++;
      subDataBeanStore.put(subDataBean.getId(), subDataBean);
    }
  }

}
